// $Id: Application.java 88 2012-03-01 15:33:37Z huijzer@gmail.com $
package lime;

import lime.application.dialog.LoginDialog;
import lime.service.RepositoryService;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication {

	private RepositoryService repositoryService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.
	 * IApplicationContext)
	 */
	public Object start(IApplicationContext context) throws Exception {

		setupWorkspace();
		
		// Spring stuff
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		repositoryService = (RepositoryService) applicationContext.getBean("repositoryService");

		Display display = PlatformUI.createDisplay();

		// Present the user with a login dialog
		LoginDialog dialog = new LoginDialog(display.getActiveShell());
		if (dialog.open() != Window.OK) {
			return IApplication.EXIT_RESTART;
		}

		try {
			int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
			if (returnCode == PlatformUI.RETURN_RESTART) {
				return IApplication.EXIT_RESTART;
			} else {
				return IApplication.EXIT_OK;
			}
		} finally {
			display.dispose();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.app.IApplication#stop()
	 */
	public void stop() {

		repositoryService.disconnect();

		if (!PlatformUI.isWorkbenchRunning()) {
			return;
		}
		final IWorkbench workbench = PlatformUI.getWorkbench();
		final Display display = workbench.getDisplay();
		display.syncExec(new Runnable() {
			public void run() {
				if (!display.isDisposed())
					workbench.close();
			}
		});
	}

	private void setupWorkspace() throws Exception {

		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRoot workspaceRoot = workspace.getRoot();
		IProject project = workspaceRoot.getProject("lime");

		if (!project.exists()) {
			project.create(null);
		}
		
		project.open(null);

		IFolder dqlFolder = project.getFolder("dql");
		if (!dqlFolder.exists()) {
			dqlFolder.create(false, true, null);
		}

		IFolder xqueryFolder = project.getFolder("xquery");
		if (!xqueryFolder.exists()) {
			xqueryFolder.create(false, true, null);
		}

		dqlFolder.refreshLocal(IResource.DEPTH_INFINITE, null);
		xqueryFolder.refreshLocal(IResource.DEPTH_INFINITE, null);
	}
}
